export default {
	data() {
		return {
			shareConfig: {
				title: '隐耀家人', // 分享标题
				path: '/pages/welcome/welcome', // 分享页路径 需要以/开头，默认是小程序首页路径

				//自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，
				//支持PNG及JPG，不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
				// imageUrl: '../../static/images/icon-logo.jpg'
			}
		}
	},
	methods: {
		urlParse(url) {
			let obj = {}
			let reg = /[?&][^?&]+=[^?&]+/g
			let arr = url.match(reg)
			// ['?id=12345','&a=b']
			if (arr) {
				arr.forEach((item) => {
					let tempArr = item.substr(1).split('=')
					let key = decodeURIComponent(tempArr[0])
					let val = decodeURIComponent(tempArr[1])
					obj[key] = val
				})
			}
			return obj
		}
	},
	// 发送给朋友
	onShareAppMessage(res) {
		console.log('onShareAppMessage :>> ', {
			...res
		})

		if (res.from == 'button') {
			// 按钮触发
		} else {

			// 内置触发
			const pages = getCurrentPages() // 获取当前页面栈
			const curPage = pages[pages.length - 1] // 获取当前页面




			if (curPage.options) {

				//判断是否有token,有的话就清除token
				console.log("&&&&&&&", curPage.route);
				if (curPage.route == "pages/index/webview" || curPage.route == "/pages/code/zhiyuanzhe" || curPage.route == "pages/index/connectGame_webview") {
					let webViewUrl = curPage.options.link;
					if (webViewUrl.indexOf('%') > -1) {
						webViewUrl = decodeURIComponent(webViewUrl);
					}
					let params = this.urlParse(webViewUrl);
					console.log("3333333", curPage.options);
					if (params && (params.l == 1 || params.l == 2 || params.l == 3)) {
						// if (params && params.l) {
						delete params.token

						let new_link = "";
						for (var item in params) {
							if (new_link == "") {
								new_link = `?${item}=${params[item]}`
							} else {
								new_link += `&${item}=${params[item]}`
							}
						}
						curPage.options.link = encodeURIComponent(res.webViewUrl.split("?")[0] + new_link);
						// curPage.options.link = res.webViewUrl.split("?")[0] + new_link;
					}

					// //h5分享方法
					// if (curPage.data) {
					// 	if (curPage.data.shareOptionsObj.path) {
					// 		console.log("测试", curPage.data.shareOptionsObj)
					// 		let that = curPage.data;

					// 		if (that.apiinfo.api) {
					// 			that.apirequest().then(res => {

					// 				if (res.data.info) {
					// 					uni.showModal({
					// 						title: '温馨提示',
					// 						content: res.data.info,
					// 						showCancel: false,
					// 						confirmText: '知道了'
					// 					})
					// 				}
					// 				// return that.shareOptionsObj;  

					// 			}).catch(err => {
					// 				// return that.shareOptionsObj;  
					// 			})
					// 		}
					// 		// curPage.options.link = that.shareOptionsObj.path;
					// 	}

					const promise = new Promise(resolve => {
						setTimeout(() => {
							const pages1 = getCurrentPages() // 获取当前页面栈
							const curPage1 = pages1[pages1.length - 1] // 获取当前页面
							if (curPage1.data.shareOptionsObj.path) {

								let that = curPage1.data;
								if (that.apiinfo.api) {
									that.apirequest().then(res => {

										if (res.data.info) {
											uni.showModal({
												title: '温馨提示',
												content: res.data.info,
												showCancel: false,
												confirmText: '知道了'
											})
										}
										// return that.shareOptionsObj;  

									}).catch(err => {
										// return that.shareOptionsObj;  
									})
								}

								resolve(curPage1.data.shareOptionsObj);
							}
							resolve(this.shareConfig);
						}, 100)

					})
					this.shareConfig.promise = promise;
					// }


				}

				let queryList = []
				for (const [k, v] of Object.entries(curPage.options)) {
					queryList.push(`${k}=${v}`)
				}
				console.log("!!!!@@@@@", queryList);
				this.shareConfig.path = `/${curPage.route}?${queryList.join('&')}` // 需要以/开头
			} else {
				this.shareConfig.path = `/${curPage.route}` // 需要以/开头
			}


			// if (curPage.data.shareOptionsObj.path) {
			// 	console.log("1",curPage.data.shareOptionsObj)
			// 	return curPage.data.shareOptionsObj
			// } else {
			// 	console.log("2",this.shareConfig)
			// 	return this.shareConfig
			// }
			console.log("分享链接", this.shareConfig);
			return this.shareConfig



		}
	}
}